import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

ApplicationWindow {
    id: testWindow
    visible: true
    width: 800
    height: 600
    title: "远程控制测试"

    // 测试属性
    property string currentRobot: "GZJY001"
    
    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 20

        Label {
            text: "远程控制功能测试"
            font.pixelSize: 24
            font.bold: true
            Layout.alignment: Qt.AlignHCenter
        }

        GroupBox {
            title: "测试说明"
            Layout.fillWidth: true

            Label {
                text: "1. 点击'启用键盘控制'按钮激活键盘监听\n" +
                      "2. 按下W/A/S/D键测试控制命令发送\n" +
                      "3. 查看日志文件验证命令是否正确记录"
                wrapMode: Text.WordWrap
                font.pixelSize: 14
            }
        }

        GroupBox {
            title: "控制面板"
            Layout.fillWidth: true

            ColumnLayout {
                anchors.fill: parent
                spacing: 15

                Button {
                    id: enableKeysButton
                    text: "启用键盘控制"
                    Layout.fillWidth: true
                    Layout.preferredHeight: 40
                    onClicked: {
                        testWindow.forceActiveFocus()
                        console.log("键盘控制已启用，请按下W/A/S/D键测试")
                    }
                }

                Rectangle {
                    Layout.fillWidth: true
                    Layout.preferredHeight: 200
                    color: "#f0f0f0"
                    border.color: "#ccc"
                    radius: 8

                    Label {
                        anchors.centerIn: parent
                        text: "W\nA S D"
                        font.pixelSize: 24
                        horizontalAlignment: Text.AlignHCenter
                    }
                }

                Label {
                    text: "当前机器人: " + currentRobot
                    font.pixelSize: 16
                }
            }
        }

        GroupBox {
            title: "测试日志"
            Layout.fillWidth: true
            Layout.fillHeight: true

            ScrollView {
                anchors.fill: parent

                TextArea {
                    id: logArea
                    readOnly: true
                    text: "测试日志将显示在这里...\n"
                    font.family: "Courier New"
                }
            }
        }
    }

    // 键盘事件处理
    Keys.onPressed: {
        var direction = ""
        switch (event.key) {
            case Qt.Key_W:
                direction = "forward"
                event.accepted = true
                break
            case Qt.Key_S:
                direction = "backward"
                event.accepted = true
                break
            case Qt.Key_A:
                direction = "left"
                event.accepted = true
                break
            case Qt.Key_D:
                direction = "right"
                event.accepted = true
                break
            default:
                return
        }
        
        if (direction !== "") {
            logArea.text += "按键: " + direction + " (Key=" + event.key + ")\n"
            
            // 模拟发送控制命令
            var command = {
                "type": "control",
                "command": direction,
                "timestamp": Date.now()
            }
            
            logArea.text += "发送命令: " + JSON.stringify(command) + "\n"
            
            // 如果robotManager可用，实际发送命令
            if (typeof robotManager !== 'undefined') {
                var success = robotManager.sendControlCommand(currentRobot, command)
                logArea.text += "发送结果: " + (success ? "成功" : "失败") + "\n"
            } else {
                logArea.text += "注意: robotManager不可用，仅记录日志\n"
            }
        }
    }

    Component.onCompleted: {
        console.log("远程控制测试窗口已启动")
        logArea.text += "远程控制测试已启动\n"
        logArea.text += "请先点击'启用键盘控制'按钮，然后按下W/A/S/D键进行测试\n"
    }
}